<?php
// +----------------------------------------------------------------------
// | Author: 发仔 <i@fazi.me> 
// +----------------------------------------------------------------------

namespace app\admin\controller;

use think\Db;
use think\Cache;

class Car extends Base
{
    protected $id = 'car_id';
	protected $table = 'car';
	
    public function index() {
		
		$op = input('param.op');
		//搜索
		$where = [];
		$car_type_id = input('param.car_type_id/d');
		$number = input('param.number');
		$name = input('param.name');
		
		$car_type_id && $where['car_type_id']  = $car_type_id;
		$number && $where['c.number']  = ['LIKE',"%$number%"];
		$name && $where['r.name']  = ['LIKE',"%$name%"];
		$this->options($where);
		
		//POPUP
		$this->assign('op',$op);
		if($op) {
			$where['c.state'] = 1;
			$this->assign('notPopup',0);
		}
		
		$SQL = Db::name($this->table)->alias('c')
				->join('car_rental r','c.car_rental_id = r.car_rental_id','left');
		//关联 
		if(in_array($op, ['bind','unbind']) ) {
			$driver_id = input('param.driver_id');
			$inornot = $op == 'bind' ? 'not in' : 'in';
			$SQL = $SQL->where('car_id',$inornot,function($query) use($driver_id) {
			    $query->name('DriverCar')->where('driver_id',$driver_id)->whereNull('delete_time')->field('car_id');
			});
		}	
		
		$list = $SQL->where($where)->whereNull('c.delete_time')
				->field('c.*,r.name AS car_rental_name')->fetchSql(0)->paginate(20);
//		halt($list);
		$this->assign('list',$list);
		$this->assign('pager',$list->render());
		
		
		return $this->out();
		
	}
	
	//品牌，车系，车型
	public function brands() {
		
		$id = input('param.id/d');
		$car_brands = Db::name('car_brand')->where('parent_id',$id)->fetchSql(0)->column('name','car_brand_id');
		
		return TplOptions($car_brands);
		
	}
	
	
	//通用选项
	protected function options($item = []) {
		
		//
//		$car_types = Cache::remember('car_types',function(){
			$car_types = Db::name('CarType')->whereNull('delete_time')->where('state',1)->select();
			$car_types = array_map('CxCarTypeTitle',$car_types);
//			return $car_types;
//		});
		
		//品牌
		$car_brands = Cache::remember('car_brands',function(){
			$car_brands = Db::name('car_brand')->where('level',1)->column('CONCAT(`type`,`name`) AS name','car_brand_id');
			return $car_brands;
		});
		//车型
		$car_models = [];
		if($item['car_brand_id']) {
			$car_models = Db::name('car_brand')->where('parent_id',$item['car_brand_id'])->fetchSql(0)->column('name','car_brand_id');
		} else {
			$car_models = Cache::remember('car_models',function(){
				$car_models = Db::name('car_brand')->where('level',2)->column('name','car_brand_id');
				return $car_models;
			});
		}
		//系列
		$car_seriess = [];
		if($item['car_model_id']) {
			$car_series = Db::name('car_brand')->where('parent_id',$item['car_model_id'])->fetchSql(0)->column('name','car_brand_id');
		} else {
			$car_series = Cache::remember('car_series',function(){
				$car_series = Db::name('car_brand')->where('level',3)->column('name','car_brand_id');
				return $car_series;
			});
		}
		//车型
		if($item['car_rental_id']) {
			$item['car_rental_name'] = Db::name('car_rental')->where('car_rental_id',$item['car_rental_id'])->fetchSql(0)->value('name');
		}
		
		if($item['car_type_id']) {
			$car_type = Db::name('car_type')->where('car_type_id',$item['car_type_id'])->fetchSql(0)->find();
			$item['car_type'] = CxCarTypeTitle($car_type)['title'];
		}
		
		//区域
		$area_ids = Db::name('area')->select();
//		$area_ids = Arr2Sibling($area_ids,0,0,'area_id','area_pid');
		$this->assign('area_ids',TplOptions($area_ids,$item['area_pid'],'area_name','area_id'));
		
		//下拉选项
		$this->assign('car_types',array_column($car_types,'title','car_type_id'));
		$this->assign('car_type_ids',TplOptions($car_types,$item['car_type_id'],'title','car_type_id'));
		$this->assign('car_brands',$car_brands);
		$this->assign('car_brand_ids',TplOptions($car_brands,$item['car_brand_id']));
		$this->assign('car_models',$car_models);
		$this->assign('car_model_ids',TplOptions($car_models,$item['car_model_id']));
		$this->assign('ar_series',$car_series);
		$this->assign('car_series_ids',TplOptions($car_series,$item['car_series_id']));
		$this->assign('energy_types',TplOptions(config('base.energy_types'),$item['car_type']));
		$this->assign('sources',TplOptions(config('base.sources'),$item['source']));
		$this->assign('structures',TplOptions(config('base.structures'),$item['structure']));
		$this->assign('gearbox_types',TplOptions(config('base.gearbox_types'),$item['gearbox_type']));
		
		return $item;
	}
}
